package com.hjm.ui.common;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.common.adapter.HorizontalTextRecycleViewAdapter;

import java.util.List;
import java.util.Map;

public class HorizontalTextList extends LinearLayout implements HorizontalTextRecycleViewAdapter.Listener {
    private List<Map<String,String>> mData;
    private RecyclerView recyclerView;
    private HorizontalTextRecycleViewAdapter horizontalTextRecycleViewAdapter;

    private OnItemClickListener onItemClickListener;
    private String selectType;

    public HorizontalTextList(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizontalText);
//        CharSequence[] textArray = a.getTextArray(R.styleable.HorizontalText_textArr);
//        if (textArray != null){
//            mData = new String[textArray.length];
//            for (int i = 0; i < textArray.length; i++)
//                mData[i] = textArray[i].toString();
//        }
        init();
    }

    private void init() {
        // 在这里添加你的布局和逻辑
        LayoutInflater.from(getContext()).inflate(R.layout.layout_horizontal_text_recycle_view, this, true);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new CustomLinearLayoutManager(this.getContext()));
        horizontalTextRecycleViewAdapter = new HorizontalTextRecycleViewAdapter(recyclerView);
        horizontalTextRecycleViewAdapter.setListener(this);
        recyclerView.setAdapter(horizontalTextRecycleViewAdapter);
    }

    public void setmData(List<Map<String,String>> mData) {
        if (mData != null) {
            this.mData = mData;
        }
        if (horizontalTextRecycleViewAdapter != null) {
            horizontalTextRecycleViewAdapter.setData(mData);
        } else {
            init();
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        if (onItemClickListener != null){
            Log.d("横向文字列表", "onItemClick: " + position);
            TextView textView = view.findViewById(R.id.text_view);
            onItemClickListener.onItemClick((String) textView.getTag(), position);
        }
    }

    @Override
    public void onViewCreated() {
        Log.d("横向文字列表", "onViewCreated: 视图创建完成回调");
        if (selectType!= null){
            setItemSelect(selectType);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(String type, int position);
    }

    public void setItemSelect(String type){
        this.selectType = type;
//        Log.d("横向文字列表", "setItemSelect: 开始设置选中 "+type+" mData.size() "+mData.size());
        for (int i = 0; i < mData.size(); i++){
            View view = recyclerView.getLayoutManager().findViewByPosition(i);
            if (view != null){
                TextView textView = view.findViewById(R.id.text_view);
                String tag = (String) textView.getTag();
//                Log.d("横向文字列表", "setSelect: tag="+tag+" 判断="+tag.equals(type));
                if (tag.equals(type)){
                    textView.setSelected(true);
                    selectType = null;
                    break;
                }else {
                    textView.setSelected(false);
                }
            } else {
                // 在调用时可能会存在适配器视图未创建完成的情况，因此需要一个视图创建完成的回调函数来重新调用此方法
                Log.d("横向文字列表", "setSelect: 未找到 view "+i);
                break;
            }
        }
    }

    public void setVisibility(boolean visibility){
        this.setVisibility(visibility ? VISIBLE : GONE);
    }
}
